package com.yykj.hospital.server.handler;

import cn.afterturn.easypoi.excel.entity.result.ExcelVerifyHandlerResult;
import cn.afterturn.easypoi.handler.inter.IExcelVerifyHandler;
import com.yykj.common.enums.HospitalCheckType;
import com.yykj.hospital.server.dto.HospitalCheckImportDTO;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;

public class HospitalCheckHandler  implements IExcelVerifyHandler<HospitalCheckImportDTO> {
    private Integer rowNum = 1;

    @Override
    public ExcelVerifyHandlerResult verifyHandler(HospitalCheckImportDTO dto) {
        StringBuilder sb = new StringBuilder();
        boolean flag = true;

        if (StringUtils.isEmpty(dto.getTypeName()) || (!"检查".equals(dto.getTypeName()) && !"检验".equals(dto.getTypeName())) ) {
            flag = false;
            sb.append("项目类型非法 ");
        }else{
            if("检查".equals(dto.getTypeName())){
                dto.setType(HospitalCheckType.check);
            }else{
                dto.setType(HospitalCheckType.examine);
            }
        }
        if (StringUtils.isEmpty(dto.getName())) {
            flag = false;
            sb.append("项目名称不能为空 ");
        }

        if (StringUtils.isEmpty(dto.getPriceStr())) {
            flag = false;
            sb.append("项目单价不能为空 ");
        }else{
            try {
                dto.setPrice(new BigDecimal(dto.getPriceStr()));
            } catch (Exception e) {
                flag = false;
                sb.append("价格格式不正确 ");
            }
        }

        if (StringUtils.isEmpty(dto.getDepCode())) {
            flag = false;
            sb.append("执行科室不能为空 ");
        }

        if (StringUtils.isEmpty(dto.getDetail())) {
            flag = false;
            sb.append("项目说明不能为空 ");
        }

        if (StringUtils.isEmpty(dto.getNotice())) {
            flag = false;
            sb.append("注意事项不能为空 ");
        }

        dto.setRowNum(rowNum++);
        dto.setErrorMsg(sb.toString());

        if (flag) {
            return new ExcelVerifyHandlerResult(true, null);
        } else {
            return new ExcelVerifyHandlerResult(false, sb.toString());
        }
    }
}
